package com.zd.attgen.service;

import java.io.IOException;
import java.io.OutputStream;
import java.time.format.DateTimeFormatter;
import java.util.List;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import com.zd.attgen.entity.Attendance;
import com.zd.attgen.entity.WorkTime.CheckinType;

@Service
public class ExcelService {

	public void export(List<Attendance> attendances, OutputStream out) throws IOException {
		XSSFWorkbook workbook = new XSSFWorkbook();
		XSSFSheet sheet = workbook.createSheet("沈阳考勤报表");
		//设置字体
		XSSFFont font = workbook.createFont();
		font.setFontName("Arial");
		font.setFontHeightInPoints((short)10);
		//设置样式
		XSSFCellStyle style = workbook.createCellStyle();
		style.setFont(font);
		style.setBorderTop(BorderStyle.THIN);
		style.setTopBorderColor((short) 0);
		style.setBorderRight(BorderStyle.THIN);
		style.setRightBorderColor((short) 0);
		style.setBorderBottom(BorderStyle.THIN);
		style.setBottomBorderColor((short) 0);
		style.setBorderLeft(BorderStyle.THIN);
		style.setLeftBorderColor((short) 0);
		style.setAlignment(HorizontalAlignment.LEFT);
		//设置周末单元格样式
		XSSFCellStyle weekEndStyle = workbook.createCellStyle();
		weekEndStyle.cloneStyleFrom(style);
		weekEndStyle.setFillForegroundColor((short) 50);
		weekEndStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		//设置假日单元格样式
		XSSFCellStyle holidayStyle = workbook.createCellStyle();
		holidayStyle.cloneStyleFrom(style);
		holidayStyle.setFillForegroundColor((short) 52);
		holidayStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		//设置出差单元格样式
		XSSFCellStyle businessTravelStyle = workbook.createCellStyle();
		businessTravelStyle.cloneStyleFrom(style);
		businessTravelStyle.setFillForegroundColor((short) 40);
		businessTravelStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		//设置请假单元格样式
		XSSFCellStyle leaveStyle = workbook.createCellStyle();
		leaveStyle.cloneStyleFrom(style);
		leaveStyle.setFillForegroundColor((short) 30);
		leaveStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		//设置列宽
		int ci = 0;
		sheet.setColumnWidth(ci++, 4000);
		sheet.setColumnWidth(ci++, 3000);
		sheet.setColumnWidth(ci++, 2000);
		sheet.setColumnWidth(ci++, 2000);
		sheet.setColumnWidth(ci++, 2000);
		sheet.setColumnWidth(ci++, 2000);
		sheet.setColumnWidth(ci++, 6000);
		//设置标题
		ci = 0;
		XSSFRow titleRow = sheet.createRow(0);
		XSSFCell cell = titleRow.createCell(ci++);
		cell.setCellStyle(style);
		cell.setCellValue("姓名");
		cell = titleRow.createCell(ci++);
		cell.setCellStyle(style);
		cell.setCellValue("日期");
		cell = titleRow.createCell(ci++);
		cell.setCellStyle(style);
		cell.setCellValue("上班时间");
		cell = titleRow.createCell(ci++);
		cell.setCellStyle(style);
		cell.setCellValue("下班时间");
		cell = titleRow.createCell(ci++);
		cell.setCellStyle(style);
		cell.setCellValue("签到时间");
		cell = titleRow.createCell(ci++);
		cell.setCellStyle(style);
		cell.setCellValue("签退时间");
		cell = titleRow.createCell(ci++);
		cell.setCellStyle(style);
		cell.setCellValue("备注");
		//冻结首行
		sheet.createFreezePane(0, 1);
		//设置打开筛选
		CellRangeAddress c = CellRangeAddress.valueOf("A1:G1");
		sheet.setAutoFilter(c);
		//设置内容
		for (int ri = 1; ri < attendances.size(); ri++) {
			XSSFRow row = sheet.createRow(ri);
			row.setHeightInPoints((float) 12.75);
			Attendance attendance = attendances.get(ri);
			ci = 0;
			cell = row.createCell(ci++);
			cell.setCellStyle(style);
//			XSSFCellStyle s = workbook.createCellStyle();
//			s.setFillForegroundColor((short) ri);
//			s.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//			cell.setCellStyle(s);
			cell.setCellValue(attendance.getAttendees().getCnname());
			cell = row.createCell(ci++);
			cell.setCellStyle(style);
			cell.setCellValue(attendance.getWorkTime().getDate().format(DateTimeFormatter.ofPattern("yyyy/M/d")));
			cell = row.createCell(ci++);
			cell.setCellStyle(style);
			cell.setCellValue(attendance.getWorkTime().getOpenTime().format(DateTimeFormatter.ofPattern("HH:mm")));
			cell = row.createCell(ci++);
			cell.setCellStyle(style);
			cell.setCellValue(attendance.getWorkTime().getCloseTime().format(DateTimeFormatter.ofPattern("HH:mm")));
			switch (attendance.getWorkTime().getCheckinType()) {
			case weekend:
				cell = row.createCell(ci++);
				cell.setCellStyle(weekEndStyle);
				cell.setCellValue("周末");
				cell = row.createCell(ci++);
				cell.setCellStyle(weekEndStyle);
				cell.setCellValue("周末");
				break;
			case holiday:
				cell = row.createCell(ci++);
				cell.setCellStyle(holidayStyle);
				cell.setCellValue("假日");
				cell = row.createCell(ci++);
				cell.setCellStyle(holidayStyle);
				cell.setCellValue("假日");
				break;
			case businessTravel:
				cell = row.createCell(ci++);
				cell.setCellStyle(businessTravelStyle);
				cell.setCellValue("出差");
				cell = row.createCell(ci++);
				cell.setCellStyle(businessTravelStyle);
				cell.setCellValue("出差");
				break;
			case leave:
				cell = row.createCell(ci++);
				cell.setCellStyle(leaveStyle);
				cell.setCellValue("请假");
				cell = row.createCell(ci++);
				cell.setCellStyle(leaveStyle);
				cell.setCellValue("请假");
				break;
			case normal:
			case feed:
			default:
				cell = row.createCell(ci++);
				cell.setCellStyle(style);
				cell.setCellValue(attendance.getCheckinTime().format(DateTimeFormatter.ofPattern("HH:mm")));
				cell = row.createCell(ci++);
				cell.setCellStyle(style);
				cell.setCellValue(attendance.getSignoffTime().format(DateTimeFormatter.ofPattern("HH:mm")));
				break;
			}
			cell = row.createCell(ci++);
			cell.setCellStyle(style);
		}
		
		sheet = workbook.createSheet("沈阳考勤明细");
		//设置列宽
		ci = 0;
		sheet.setColumnWidth(ci++, 4000);
		sheet.setColumnWidth(ci++, 4000);
		//设置标题
		ci = 0;
		titleRow = sheet.createRow(0);
		cell = titleRow.createCell(ci++);
		cell.setCellStyle(style);
		cell.setCellValue("姓名");
		cell = titleRow.createCell(ci++);
		cell.setCellStyle(style);
		cell.setCellValue("出勤时间");
		//冻结首行
		sheet.createFreezePane(0, 1);
		//设置打开筛选
		c = CellRangeAddress.valueOf("A1:B1");
		sheet.setAutoFilter(c);
		//设置内容
		for (int i = 1, ri = 1; i < attendances.size(); i++) {
			Attendance attendance = attendances.get(i);
			if(CheckinType.normal != attendance.getWorkTime().getCheckinType() && CheckinType.feed != attendance.getWorkTime().getCheckinType()) {
				continue;
			}
			String date = attendance.getWorkTime().getDate().format(DateTimeFormatter.ofPattern("yyyy/M/d"));
			String ciTime = attendance.getCheckinTime().format(DateTimeFormatter.ofPattern("HH:mm"));
			String soTime = attendance.getSignoffTime().format(DateTimeFormatter.ofPattern("HH:mm"));
			XSSFRow row = sheet.createRow(ri++);
			row.setHeightInPoints((float) 12.75);
			ci = 0;
			cell = row.createCell(ci++);
			cell.setCellStyle(style);
			cell.setCellValue(attendance.getAttendees().getCnname());
			cell = row.createCell(ci++);
			cell.setCellStyle(style);
			cell.setCellValue(date + " " + ciTime);
			
			row = sheet.createRow(ri++);
			row.setHeightInPoints((float) 12.75);
			ci = 0;
			cell = row.createCell(ci++);
			cell.setCellStyle(style);
			cell.setCellValue(attendance.getAttendees().getCnname());
			cell = row.createCell(ci++);
			cell.setCellStyle(style);
			cell.setCellValue(date + " " + soTime);
		}
		
		try {
			workbook.write(out);
		} catch (IOException e) {
			throw e;
		} finally {
			workbook.close();
		}
	}
	
}
